home *** CD-ROM | disk | FTP | other *** search
- Path: news.clark.net!not-for-mail
- From: gusty@clark.net (Harlan Messinger)
- Newsgroups: comp.lang.c++
- Subject: Re: Why use private class members instead of protected?
- Date: 11 Jan 1996 19:56:10 GMT
- Organization: Clark Internet Services, Inc., Ellicott City, MD USA
- Message-ID: <4d3q0q$uj@clarknet.clark.net>
- References: <30F4AB49.6ABB@sierra.net>
- NNTP-Posting-Host: explorer.clark.net
- Mime-Version: 1.0
- Content-Type: TEXT/PLAIN; charset=ISO-8859-1
- Content-Transfer-Encoding: 8bit
- X-Newsreader: TIN [UNIX 1.3 950726BETA PL0]
-
- TGColwell (snowbull@sierra.net) wrote:
- : I'm relatively new to c++. I have one quick question: If child
- : classes can only access protected members of the parent class,
- : why make any members of any class private? Wouldn't it be
- : better to make members of the parent class protected so that the
- : class is alway "inheritance ready"?
- :
-
- No.
-
- Suppose I market a Date class. I allow the date to be set through a
- public or protected function setdate(int m, int d, int y), or perhaps
- through a similar overloaded assignment operator and a copy constructor.
- I allow you to retrieve the date through a variety of functions in a
- number of formats, both string and numeric.
-
- I store the date in three separate member variables: mon_, day_ and year_.
-
-
- Suppose you wrote code that referred to mon_, day_ and year_. What would
- you do when the next version of my library, which you load over the old
- one, stores the date as a Julian long integer, and no longer HAS this
- threesome?
-
- My Date class has a variety of functions that return results like "first
- day of the month following this date" and "number of days from this date
- to another date". I have tuned these functions so that they always work
- correctly--IF the Date object is in a legal state. To assure that this is
- true, I have implemented data checking so that if you try to execute, for
- example, setdate(42, 49, -3), an exception will be produced. But if you
- could just go setting mon_ on your own, there would be nothing to stop
- you from setting it to 42.
-
- In other words, it's none of your business whether my class contains mon_
- or julian_date or any other implementation detail. These are private.
- Your only concern is with the protected and public data (if any) and
- functions that give my class the facilities that it is supposed to
- provide you with.
-